// pages/avatar/index.js
const requsest=require('../../utils/request')
const openId=wx.getStorageSync('openid')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    newimg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'options');
    console.log(wx.getStorageSync('userInfo'),'userinfo')
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  // 点击上传图片
  uploadAvatar: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        console.log(res,'uploadavatar-res')
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImageShop('album'); //从相册中选择
          } else if (res.tapIndex == 1) {
            that.chooseWxImageShop('camera'); //手机拍照
          }
        }
      }
    })

  },

  //选择图片
  chooseWxImageShop(type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: (res)=>{
        console.log(res),'chooseWxImageShop-res'
        const tempFilePaths = res.tempFilePaths[0]
          //上传图片到服务器
        wx.uploadFile({
          filePath: tempFilePaths, 
          name: 'avator',
          url: 'http://10.3.221.9:7001/api/uploadimg',
          header: {
            'content-type': 'multipart/form-data',
          }, // 设置请求的 header
          data: {
            'filePath': tempFilePaths[0],
          },
          success:(res)=> {
            // console.log(res,'uploadimg-res')
            var {imgUrl}=JSON.parse(res.data) 
            console.log(imgUrl,'imgurl')
            this.setData({
              userInfo:{
                ...this.data.userInfo,
                avatarUrl:imgUrl
              },
              newimg:imgUrl
            })
            console.log(this.data.userInfo,'this.data.userInfo')
          },
          fail: function (res) {
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
            console.log(tempFilePaths)
          }
        })
      }
    })

  },

//确定
handleConfirm(value){
  console.log(value.detail.value,'value')
  requsest({
    url:'/api/edmitwxuser',
    method:'put',
    data:{
      ...value.detail.value,
      avatarUrl:this.data.newimg,
      openId:wx.getStorageSync('openid'),
    }
  }).then(res=>{
    console.log(res)
    if(res.data.code===1){
      wx.showToast({
        title: '更新成功'
      })
      const pages=getCurrentPages()
      console.log(this.data.newimg)
      if(this.data.newimg){
        wx.setStorageSync('userInfo', {...wx.getStorageSync('userInfo'),avatarUrl:this.data.newimg,...value.detail.value})
      }else{
        wx.setStorageSync('userInfo', {...wx.getStorageSync('userInfo'),...value.detail.value})
      }
      
      console.log(pages,'pages')
      wx.navigateBack({
        delta:1
      })
    }
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})